﻿#include "wifiwidget.h"

WiFiWidget::WiFiWidget(QListWidgetItem *pItem,QWidget *parent)
    : QWidget{parent}
{
    this->m_pItem = pItem;
    m_connected = false;
    m_pLoadingLabel = new LoadingLabel(this);
    m_pLoadingLabel->hide();
    this->setWindowFlags(Qt::FramelessWindowHint);//无边框
    status = LEAVE;//默认隐藏全部控件
    // 创建 QLabel 用于显示名称和图片
    m_name_label = new QLabel(this);
    m_pLabEditTip = new QLabel(this);
    //创建提示标签
    m_pLabEditTip->setText("输入网络安全密钥");
    m_pLabEditTip->hide();

    //创建密码编辑框
    m_pEditPasswd = new QLineEdit(this);
    m_pEditPasswd->hide();
    m_wifi_img = new QLabel(this);
    // 加载图片
    QPixmap pix = QPixmap(":/img/wifi.png");
    // 设置图片显示大小
    m_wifi_img->setPixmap(pix.scaled(60, 60, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    // 确保图片按比例缩放且不失真
    m_wifi_img->setScaledContents(false);

    // 创建垂直布局
    m_pLayoutMain = new QVBoxLayout(this);
    //水平布局
    m_pLayoutHBox = new QHBoxLayout;
    // 添加图片到布局
    m_pLayoutHBox->addWidget(m_wifi_img, 0, Qt::AlignLeft | Qt::AlignTop);

    // 添加名称标签到布局
    m_pLayoutHBox->addWidget(m_name_label, 0, Qt::AlignLeft | Qt::AlignTop);

    // 添加拉伸，使内容靠左对齐
    m_pLayoutHBox->addStretch();

    // 设置布局的间距和边距
    m_pLayoutHBox->setSpacing(10); // 可以根据需要调整间距
    m_pLayoutHBox->setMargin(10);  // 可以根据需要调整边距
    // 勾选框
    m_pCheckBox = new QCheckBox;
    m_pCheckBox->setObjectName("WifiListItem_CheckBox");
    m_pCheckBox->setText(tr("自动连接"));
    m_pCheckBox->setChecked(true);
    m_pCheckBox->hide();
    // wifi按钮
    m_pBtnWifi = new QPushButton;
    m_pBtnWifi->setText("连接");
    m_pBtnWifi->hide();

    //创建下一步按钮和取消按钮
    m_pBtnNext = new QPushButton;
    m_pBtnNext->setText(tr("下一步"));
    m_pBtnNext->hide();
    m_pBtnCancel = new QPushButton;
    m_pBtnCancel->setText("取消");
    m_pBtnCancel->hide();
    //创建已连接按钮
    m_pBtnConnected = new QPushButton;
    m_pBtnConnected->setText(tr("断开连接"));
    m_pBtnConnected->hide();
    //放入水平布局
    m_pLayoutHBox_button = new QHBoxLayout;
    m_pLayoutHBox_button->addWidget(m_pBtnNext);
    m_pLayoutHBox_button->addWidget(m_pBtnCancel);
    m_pLayoutMain->addLayout(m_pLayoutHBox);
    m_pLayoutMain->addWidget(m_pLabEditTip);//添加密码输入提示
    m_pLayoutMain->addWidget(m_pEditPasswd);//添加密码输入框
    m_pLayoutMain->addLayout(m_pLayoutHBox_button); //添加按钮水平布局
    m_pLayoutMain->addWidget(m_pCheckBox);//添加复选框
    m_pLayoutMain->addWidget(m_pBtnConnected);
    m_pLayoutMain->addWidget(m_pBtnWifi);//添加WiFi按钮
    m_pLayoutMain->addWidget(m_pLoadingLabel);//添加加载的label
    m_pLayoutMain->addWidget(m_pBtnConnected);


    //设置点击连接按钮后的事件
    connect(m_pBtnWifi, SIGNAL(clicked()), this, SLOT(connect_wifi()));
    //设置点击下一步后的事件
    connect(m_pBtnNext, SIGNAL(clicked()), this, SLOT(click_next()));
    //设置点击取消按钮后的事件
    connect(m_pBtnCancel, SIGNAL(clicked()), this, SLOT(click_cancel()));
}

void WiFiWidget::SetWiFIInfo(WiFiInfo& wifi_info)
{
    QString wifi_name = QString::fromUtf16((const ushort *)wifi_info.cstrSSID.c_str());
    this->m_wifi_info = wifi_info;
    // 设置名称文本
    m_name_label->setText(wifi_name);
}

//设置状态
Q_INVOKABLE void WiFiWidget::SetStatus(STATUS status)
{
    //如果为state为true，则展示连接按钮和自动连接复选框
    switch(status){
    //当选择此WiFi时，显示连接按钮和复选框
    case ENTER:
    {
        m_pCheckBox->show();
        m_pBtnWifi->show();
        m_pLabEditTip->hide();
        m_pEditPasswd->hide();//显示密码输入框
        m_pBtnNext->hide();//显示下一步按钮
        m_pBtnCancel->hide();//显示取消按钮
        m_pBtnConnected->hide();//取消已经连接按钮
        break;
    }
    //点击连接按钮后，显示输入框和提示词
    case CONNECT:
    {
        m_pCheckBox->hide();//隐藏复选框
        m_pBtnWifi->hide();//隐藏连接按钮
        m_pLabEditTip->show();//显示提示
        m_pEditPasswd->show();//显示密码输入框
        m_pBtnNext->show();//显示下一步按钮
        m_pBtnCancel->show();//显示取消按钮
        m_pBtnConnected->hide();//取消已经连接按钮
        break;
    }
    //WIFI连接完成时候,显示已连接按钮
    case COMPLETE:
    {
        //m_pLoadingLabel->hide();
        //this->m_pItem->setSizeHint(QSize(0, 200));
        QMetaObject::invokeMethod(m_pLoadingLabel, "hide", Qt::QueuedConnection);
        m_pLabEditTip->hide();
        m_pEditPasswd->hide();
        m_pBtnNext->hide();
        m_pBtnCancel->hide();
        //m_pBtnConnected->show();
        QMetaObject::invokeMethod(m_pBtnConnected, "show", Qt::QueuedConnection);
        this->m_pBtnWifi->setText("断开连接");
        this->m_connected = true;
        //this->m_pItem->setSizeHint(QSize(0, 200));
        break;
    }
    //选择其他WiFi时候,隐藏其他不需要的控件
    case LEAVE:
    {
        m_pLoadingLabel->hide();
        m_pCheckBox->hide();
        m_pBtnWifi->hide();
        m_pLabEditTip->hide();
        m_pEditPasswd->hide();
        m_pBtnNext->hide();
        m_pBtnCancel->hide();
        m_pBtnConnected->hide();//取消已经连接按钮
        break;
    }


    }


}

//设置GUID，是网卡唯一标识符，连接WiFi，断开WiFi等操作需要
void WiFiWidget::SetGuid(GUID guid)
{
    this->m_guid = guid;
}

Q_INVOKABLE void WiFiWidget::stopAnimation()
{
    m_pLoadingLabel->stopAnimation_();
    qDebug()<<"开始测试";
    //m_pLayoutMain->removeWidget(m_pLoadingLabel);
    //delete m_pLoadingLabel;
}




//点击连接WiFi
void WiFiWidget::connect_wifi()
{
    // qDebug()<<"connect_wifi";
    //如果此WiFi还没有连接成功
    if(!m_connected){
        SetStatus(CONNECT);
        this->m_pItem->setSizeHint(QSize(0, 250));
    }
    //操作为断开WiFi
    else{
        this->m_pItem->setSizeHint(QSize(0, 200));
    }

}

//点击下一步按钮，进行wifi连接
void WiFiWidget::click_next()
{
    qDebug()<<"点击连接";
    SetStatus(LEAVE);
    //WiFiManager::GetInstance().RigisterConnectNotification(CONNECT_NOTIFICATION_CALLBACK(OnNotificationCallback));



    m_pLoadingLabel->show();
    m_pLoadingLabel->startAnimation_();

    QString password = "zha123456";
    WiFiManager& wifi_manager = WiFiManager::GetInstance();
    wifi_manager.ConnectWiFi(m_wifi_info,password.toStdWString(),m_guid,0);
    // QString password = "cj666666";
    // WiFiManager& wifi_manager = WiFiManager::GetInstance();
    // wifi_manager.ConnectWiFi(m_wifi_info,password.toStdWString(),m_guid,0);
    qDebug()<<"连接完成";
}

//点击取消按钮,回到上一步
void WiFiWidget::click_cancel()
{
    qDebug()<<"回到上一步";
    SetStatus(ENTER);
}

void WiFiWidget::OnWlanConnectCallback(PWLAN_CONNECTION_NOTIFICATION_DATA pData, DWORD dwConnectCode)
{
    if (pData)
    {
        std::wstring cstrSSID = pData->strProfileName;
        switch (dwConnectCode)
        {
        case wlan_notification_acm_connection_start:
            qDebug() << "开始连接\r\n";
            break;
        case wlan_notification_acm_connection_complete:
        {
            if (pData->wlanReasonCode == WLAN_REASON_CODE_SUCCESS)
            {
                qDebug()<<"连接成功1111\r\n";
                //emit wifiwidget_current->complete_();
                //wifiwidget_current->stopAnimation();
                //wifiwidget_current->SetStatus(COMPLETE);
                // wifiwidget_current

            }
            else
            {
                WCHAR wcReasonCode[100] = { 0 };
                WlanReasonCodeToString(pData->wlanReasonCode, 100, wcReasonCode, NULL);
                qDebug() << "连接失败，"; //以这个成功失败作为结果上传
                std::wstring cstrReason = wcReasonCode;
            }
        }
        break;
        case wlan_notification_acm_connection_attempt_fail: //尝试连接失败后会，系统还会发送wlan_notification_acm_connection_complete，标注失败原因
            qDebug()<< "尝试连接失败\r\n";
            break;
        case wlan_notification_acm_disconnecting:
            qDebug() << "正在断开\r\n";
            break;
        case wlan_notification_acm_disconnected:
            qDebug() << "已经断开\r\n";
            break;
        default:
            break;
        }

    }
    else
    {
        switch (dwConnectCode)
        {
        case wlan_notification_acm_scan_complete:
            qDebug()<< "扫描完成\r\n";
            break;
        case wlan_notification_acm_scan_fail:
            qDebug()<< "扫描失败\r\n";
            break;
        default:
            break;
        }
    }
}


